import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Hbase {

    public static void main(String[] args) throws IOException {
        System.out.println(isTableExist("person"));
//        createTable("su", "info");
//        dropTable("su");
//        addRowData("person","1003","info", "name","lisi");
//        deleteMultiRow("person", "1003");
//        getAllRows("person");
        getRowQualifier("person", "1002", "info", "name");
    }
    public static Configuration conf;
    static{
        //使用HBaseConfiguration的单例方法实例化
        conf = HBaseConfiguration.create();
        conf.set("HBase.zookeeper.quorum", "master");
        conf.set("HBase.zookeeper.property.clientPort", "2181");
    }
    public static boolean isTableExist(String tableName) throws MasterNotRunningException,
            ZooKeeperConnectionException, IOException{
        //在HBase中管理、访问表需要先创建HBaseAdmin对象
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();


        return admin.tableExists(tableName);
    }
    public static void createTable(String tableName, String... columnFamily) throws
            MasterNotRunningException, ZooKeeperConnectionException, IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin =(HBaseAdmin) connection.getAdmin();
        //判断表是否存在
        if(isTableExist(tableName)){
            System.out.println("表" + tableName + "已存在");
            //System.exit(0);
        }else{
            //创建表属性对象,表名需要转字节
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            for(String column:columnFamily){
                descriptor.addFamily(new HColumnDescriptor(column));
            }
            admin.createTable(descriptor);
        }
    }
    public static void dropTable(String tableName) throws MasterNotRunningException,
            ZooKeeperConnectionException, IOException{
        Connection connection=ConnectionFactory.createConnection(conf) ;
        Admin admin = (HBaseAdmin)connection.getAdmin();
        if(isTableExist(tableName)){
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("表" + tableName + "删除成功！");
        }else{
            System.out.println("表" + tableName + "不存在！");
        }
    }
    public static void addRowData(String tableName, String rowKey, String columnFamily, String
            column, String value) throws IOException{
        //创建HTable对象
        Connection connection = ConnectionFactory.createConnection(conf);
        Table hTable = connection.getTable(TableName.valueOf(tableName));
        //向表中插入数据
        Put put = new Put(rowKey.getBytes(StandardCharsets.UTF_8));
        //向Put对象中组装数据
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        hTable.put(put);
        hTable.close();
        System.out.println("插入数据成功");
    }
    public static void deleteMultiRow(String tableName, String... rows) throws IOException{
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));

        ArrayList<Delete> list = new ArrayList<>();
        for(String row : rows){
            Delete delete = new Delete(row.getBytes());
            list.add(delete);
        }
        table.delete(list);
        table.close();
    }
    public static void getAllRows(String tableName) throws IOException{
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));
        //得到用于扫描region的对象
        Scan scan = new Scan();
        //使用HTable得到resultscanner实现类的对象
        ResultScanner scanner = table.getScanner(scan);
        for (Result result:scanner){
            Cell[] cells = result.rawCells();
            for (Cell cell:cells
                 ) {
                System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列族
                System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }
    public static void getRowQualifier(String tableName, String rowKey, String family, String
            qualifier) throws IOException{
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell cell : cells){
            System.out.println("行键:" + Bytes.toString(result.getRow()));
            System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

}
